const app=getApp();
Page({
  data: {
    list: [],
    loading: false,
    hasMore: true,
    pageNum: 1,
    pageSize: 10,
    nickNameFilter: '',phoneFilter:'',wechatFilter:''
  },

  onLoad() {
    this.loadList();
  },

  onSearchChange(e) {
    this.setData({ 'searchParams.nickName': e.detail.value });
  },

  onPhoneInput(e) {
    this.setData({ 'searchParams.phone': e.detail.value });
  },

  onSearch() {
    this.setData({ pageNum: 1 });
    this.loadList();
  },

  exportExcel() {
    // 调用后端接口导出 Excel
    wx.showToast({ title: '正在导出...' });
  },

  loadList() {
    if (this.data.loading) return;

    this.setData({ loading: true });

    const payload= {
      nickName:this.data.nickNameFilter,
      phone:this.data.phoneFilter,
      wechat:this.data.wechatFilter,
      page: this.data.pageNum,
      limit: this.data.pageSize
    };
    // 模拟 API 请求
    app.request.post(  '/customer/business/list',    payload ,     (res) => {
        console.log("列表查询返回:",res);
        const newData = res.data.records || [];
        newData.forEach(p=>{
          // 解密手机号和微信号
          p.phone=app.decryptData(p.phone);
          p.wechat=app.decryptData(p.wechat);
        });
        const newList = this.data.pageNum === 1 ? newData : [...this.data.list, ...newData];
        this.setData({
          list: newList,
          hasMore: newData.length === this.data.pageSize,
          loading: false,
          pageNum: this.data.pageNum + 1
        });
      },       
      (err) => {
        this.setData({ loading: false });
      });
  },

  loadMore() {
    if (!this.data.hasMore || this.data.loading) return;
    this.loadList();
  },

  onEdit(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({ url: `/pages/operation/edit?id=${id}` });
  },

  onDelete(e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认删除？',
      success: (res) => {
        if (res.confirm) {
          // 删除逻辑
          wx.showToast({ title: '删除成功' });
        }
      }
    });
  },

  onFollowUp(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({ url: `/pages/followRecord/followRecord?id=${id}` });
  }
});
